﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using System.Threading;
using ModemGSM;
using PhoneManager.LanguageText;
namespace PhoneManager
{
    public partial class FrmModemSet : FrmDefault
    {
        public bool PerformAutoDect { get; set; }
        public bool PerformConect { get; set; }
        public FrmModemSet()
        {
            InitializeComponent();
        }
        private void LoadAllPorts()
        {
            string[] ports = System.IO.Ports.SerialPort.GetPortNames();
            cboComPort.Items.Clear();
            cboComPort.Items.AddRange(ports);
            cboComPort.Items.Insert(0,ResourcesText.WordSelect +  "...");
            cboComPort.SelectedIndex = 0;
        }
        private void FrmModemSet_Load(object sender, EventArgs e)
        {
            cboBaudRate.SelectedIndex = 0;
            cboDataBit.SelectedIndex = 0;
            cboStopBit.SelectedIndex = 0;
            cboFlowControl.SelectedIndex = 0;
            LoadAllPorts();
        }
        private void InitDefaultModem()
        {

            if (cboComPort.SelectedIndex > 0)
            {
                SharedLib.ModemGSM.PortName = cboComPort.Text;
            }
            SharedLib.ModemGSM.BaudRate = int.Parse(cboBaudRate.Text);
            SharedLib.ModemGSM.StopBits = StopBits.One;
            if (cboStopBit.Text == "2")
            {
                SharedLib.ModemGSM.StopBits = StopBits.Two;
            }
            else if (cboStopBit.Text == "1.5")
            {
                SharedLib.ModemGSM.StopBits = StopBits.OnePointFive;
            }
            SharedLib.ModemGSM.Handshake = Handshake.RequestToSend;
            if (cboFlowControl.Text == "None")
            {
                SharedLib.ModemGSM.Handshake = Handshake.None;
            }
            else if (cboFlowControl.Text == "Xon/Xoff")
            {
                SharedLib.ModemGSM.Handshake = Handshake.XOnXOff;
            }
            SharedLib.ModemGSM.ReadTimeout = Properties.Settings.Default.DefaultReadTimeout;
            SharedLib.ModemGSM.WriteTimeout = Properties.Settings.Default.DefaultWriteTimeout;
            SharedLib.ModemGSM.OpenTimeOut = Properties.Settings.Default.DefaultOpenTimeOut;
        }
        private void BntAutoDetect_Click(object sender, EventArgs e)
        {
            if (BntAutoDetect.Text == ResourcesText.WordClear)
            {
                SharedLib.ReadInfoModem = false;
                BntOk.Enabled = true;
                BntCancel.Enabled = true;
                BntAutoDetect.Enabled = true;
                LoadAllPorts();
                BntAutoDetect.Text = ResourcesText.AutoDetc;
                cboComPort.SelectedIndex = 0;
                return;
            }
            BntAutoDetect.Text = ResourcesText.WordClear;
            Cursor oldcur = this.Cursor;
            BntOk.Enabled = false;
            BntCancel.Enabled = false;
            BntAutoDetect.Enabled = false;
            SharedLib.ReadInfoModem = true;
            this.Cursor = Cursors.WaitCursor;
            if (SharedLib.ModemGSM.IsOpen)
            {
                SharedLib.ShowMsgApp(ResourcesText.WordDisconnecting +  "...");
                SharedLib.ModemGSM.Close();
            }

            InitDefaultModem();
            PortFound[] ports = SharedLib.ModemAutoDetect();

            this.Cursor = Cursors.Default;
            if (ports.Length == 0)
            {
                SharedLib.ShowMsgApp(ResourcesText.NotFoundPort);
                SharedLib.ReadInfoModem = false;
                BntOk.Enabled = true;
                BntCancel.Enabled = true;
                BntAutoDetect.Enabled = true;
                LoadAllPorts();
                BntAutoDetect.Text = ResourcesText.AutoDetc;
                SharedLib.MsgAlert(ResourcesText.NotFoundPort);
                cboComPort.SelectedIndex = 0;
                return;
            }
            cboComPort.Items.Clear();
            cboComPort.Items.Insert(0, ResourcesText.WordSelect + "...");
            foreach (PortFound item in ports)
            {
                cboComPort.Items.Add(item);
            }
            cboComPort.SelectedIndex = 0;
            BntOk.Enabled = true;
            BntCancel.Enabled = true;
            BntAutoDetect.Enabled = true;
            if (ports.Length == 1)
            {
                SharedLib.MsgAlert(ResourcesText.FoundPort);
                cboComPort.SelectedIndex = 1;
            }
            else
            {
                SharedLib.MsgAlert(ResourcesText.WordFound + " " + ports.Length.ToString() + " " + ResourcesText.PortConect);
            }
        }
        private void BntOk_Click(object sender, EventArgs e)
        {
            BntAutoDetect.Text = ResourcesText.AutoDetc;
            if (cboComPort.SelectedIndex <= 0)
            {
                return;
            }
            Cursor oldcur = this.Cursor;
            this.Cursor = Cursors.WaitCursor;
            BntOk.Enabled = false;
            BntCancel.Enabled = false;
            BntAutoDetect.Enabled = false;
            SharedLib.ReadInfoModem = true;
            if (SharedLib.ModemGSM.IsOpen)
            {
                SharedLib.ShowMsgApp(ResourcesText.WordDisconnecting);
                SharedLib.ModemGSM.Close();
            }
            SharedLib.ShowMsgApp(cboComPort.Text + " " + ResourcesText.Wordchecking + "...");
            SharedLib.MainForm.toolBarStatus.Text = ActiveStatusEnum.Disconnected.ToString();
            SharedLib.MainForm.toolBarStatus.ForeColor = Color.Red;
            SharedLib.MainForm.toolBarStatus.Image = Properties.Resources.Off;

            InitDefaultModem();

            SharedLib.ModemConfig.IsTested = false;
            SharedLib.ModemConfig.PortTested = "";
            SharedLib.ModemGSM.Open();
            if (!SharedLib.ModemGSM.IsOpen || !SharedLib.ModemGSM.ModemIsPhone())
            {
                this.Cursor = oldcur;
                SharedLib.ShowMsgApp(SharedLib.ModemGSM.Error);
                SharedLib.ModemGSM.Close();
                SharedLib.ReadInfoModem = false;
                BntOk.Enabled = true;
                BntCancel.Enabled = true;
                BntAutoDetect.Enabled = true;
                return;
            }
            string SerialModem = SharedLib.ModemGSM.IMEI;
            SharedLib.ModemConfig.PortTested = SharedLib.ModemGSM.PortName;
            SharedLib.ModemConfig.PortName = SharedLib.ModemGSM.PortName;
            SharedLib.ModemConfig.IsTested = true;
            SharedLib.ModemConfig.Serial = SerialModem;
            SharedLib.ModemConfig.BaudRate = SharedLib.ModemGSM.BaudRate;
            SharedLib.ModemConfig.DataBits = SharedLib.ModemGSM.DataBits;
            SharedLib.ModemConfig.Handshake = SharedLib.ModemGSM.Handshake;
            SharedLib.ModemConfig.OpenTimeOut = SharedLib.ModemGSM.OpenTimeOut;
            SharedLib.ModemConfig.ReadTimeout = SharedLib.ModemGSM.ReadTimeout;
            SharedLib.ModemConfig.StopBits = SharedLib.ModemGSM.StopBits;
            SharedLib.ModemConfig.WriteTimeout = SharedLib.ModemGSM.WriteTimeout;
            SharedLib.ReadInfoModem = true;
            SharedLib.SaveModemFileName(SharedLib.ModemConfig, SharedLib.CreateNameModemConfig(SerialModem));
            SharedLib.ModemGSM.Close();
            this.Cursor = oldcur;
            this.Close();
        }

        private void BntCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void cboComPort_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cboComPort.SelectedIndex <= 0)
            {
                LblModel.Text = "";
                BntOk.Enabled = false;
            }
            else
            {
                if (cboComPort.SelectedItem.GetType().IsInstanceOfType(new PortFound()))
                {
                    LblModel.Text = ((PortFound)cboComPort.SelectedItem).Model;
                }
                BntOk.Enabled = true;
            }
        }

        private void FrmModemSet_Shown(object sender, EventArgs e)
        {
            if (PerformAutoDect)
            {
                BntAutoDetect.PerformClick();
            }
            else if (PerformConect)
            {
                BntOk.PerformClick();
            }
        }

    }
}
